/*
 * Fast car reverse image preview module
 *
 * Copyright (C) 2015-2018 AllwinnerTech, Inc.
 *
 * Contacts:
 * Zeng.Yajian <zengyajian@allwinnertech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>

#define MODULE_NAME "car-reverse"

struct preview_params {
	int screen_width;
	int screen_height;

	int src_width;
	int src_height;
	int format;
};

struct car_reverse_private_data {
	struct preview_params preview;
	struct buffer_manager *buffer;
};

static void of_property_parse_u32(struct platform_device *pdev,
			const char *name, int *retval, unsigned int defval)
{
	if (of_property_read_u32(pdev->dev.of_node, name, retval) != 0) {
		dev_err(&pdev->dev, "missing property '%s', \
			default value %d\n", name, defval);
		*retval = defval;
	}
}

static void __parse_config(struct platform_device *pdev,
				struct car_reverse_private_data *priv)
{
	of_property_parse_u32(pdev,
		"screen_width", &priv->preview.screen_width, 0);

	of_property_parse_u32(pdev,
			"screen_height", &priv->preview.screen_height, 0);
}

static int car_reverse_probe(struct platform_device *pdev)
{
	struct car_reverse_private_data *priv;
	if (!pdev->dev.of_node) {
		dev_err(&pdev->dev, "of_node is missing\n");
		return -EINVAL;
	}

	priv = devm_kzalloc(&pdev->dev,
			sizeof(struct car_reverse_private_data), GFP_KERNEL);
	if (!priv) {
		dev_err(&pdev->dev, "kzalloc for private data failed\n");
		return -ENOMEM;
	}
	platform_set_drvdata(pdev, priv);
	__parse_config(pdev, priv);

	dev_info(&pdev->dev, "car reverse module probe\n");
	return 0;
}

static int car_reverse_remove(struct platform_device *pdev)
{
	return 0;
}

static int car_reverse_suspend(struct device *dev)
{
	return 0;
}

static int car_reverse_resume(struct device *dev)
{
	return 0;
}

static const struct dev_pm_ops car_reverse_pm_ops = {
	.suspend = car_reverse_suspend,
	.resume  = car_reverse_resume,
};

static const struct of_device_id car_reverse_dt_ids[] = {
	{ .compatible = "sunxi,car-reverse" },
	{ },
};

static struct platform_driver car_reverse_driver = {
	.probe  = car_reverse_probe,
	.remove = car_reverse_remove;
	.driver = {
		.name = MODULE_NAME,
		.pm = &car_reverse_pm_ops,
		.of_match_table = car_reverse_dt_ids,
	},
}
module_platform_driver(car_reverse_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Zeng.Yajian <zengyajian@allwinnertech.com>");
MODULE_DESCRIPTION("Sunxi fast car reverse image preview");
